package com.atguigu.java8.functional_interface;

import com.atguigu.java8.lambda.Employee;
import org.junit.Test;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

// 方法引用
public class TestMethodRef {

    // 对象：：实例方法名
    @Test
    public void test1() {
        PrintStream out = System.out;
        Consumer<String> consumer = x -> out.println(x);

        PrintStream out1 = System.out;
        Consumer<String> consumer1 = out::println;

        Consumer<String> consumer2 = System.out::println;
        consumer2.accept("Method Reference");
    }

    @Test
    public void test2() {
        Employee employee = new Employee();
        Supplier<String> supplier = () -> employee.getName();
        String name = supplier.get();
        System.out.println(name);

        Supplier<Integer> supplier1 = employee::getAge;
        Integer age = supplier1.get();
        System.out.println(age);
    }

    // 类：：静态方法名
    @Test
    public void test3() {
        Comparator<Integer> comparator = (x, y) -> Integer.compare(x, y);

        Comparator<Integer> comparator1 = Integer::compare;
    }

    // 类：：实例方法名
    @Test
    public void test4() {
        BiPredicate<String, String> biPredicate = (x, y) -> x.equals(y);

        BiPredicate<String, String> biPredicate1 = String::equals;
    }

    // 构造器引用
    @Test
    public void test5() {
        Supplier<Employee> supplier = () -> new Employee();

        Supplier<Employee> supplier1 = Employee::new;
        Employee employee = supplier1.get();
        System.out.println(employee);
    }

    @Test
    public void test6() {
        Function<Integer, Employee> function = i -> new Employee(i);

        Function<Integer, Employee> function1 = Employee::new;
        Employee employee = function1.apply(116);
        System.out.println(employee);

        BiFunction<String, Integer, Employee> function2 = Employee::new;
    }

    // 数组引用
    @Test
    public void test7() {
        Function<Integer, String[]> function = x -> new String[x];
        String[] strings = function.apply(6);
        System.out.println(strings.length);

        Function<Integer, String[]> function1 = String[]::new;
        String[] strings1 = function1.apply(20);
        System.out.println(strings1.length);
    }

}
